
from django.core.exceptions import ObjectDoesNotExist
from api.models import Package,  Material
from rbac.models import User
from api.serializers import PackageSerializer, MaterialSerializer


def package_exist(package_id):
    try:
        rows = Package.objects.filter(id=package_id)
        if len(rows) == 0:
            return False
        # print('package rows len:', len(rows))
        return True
    except ObjectDoesNotExist:
        return False
    return False


def user_exist(username):
    try:
        rows = User.objects.filter(username=username)
        if len(rows) == 0:
            return False
        # print('user rows len:', len(rows))
        return True
    except ObjectDoesNotExist:
        return False
    return False


def get_package(package_id):
    try:
        row = Package.objects.get(id=package_id)
        return PackageSerializer(row).data
    except ObjectDoesNotExist:
        return {}
    return {}


def get_material(material_id):
    try:
        row = Material.objects.get(id=material_id)
        return MaterialSerializer(row).data
    except ObjectDoesNotExist:
        return {}
    return {}





